import request from '@/utils/request'

interface GetUserListParams {
  page: number
  size: number
  keyword?: string // 添加搜索关键词参数
}

export const getUserList = (params: GetUserListParams) => {
  return request({
    url: '/manager/userlist',
    method: 'get',
    params,
  })
}

interface AddUserParams {
  userAccount: string
  username: string
  userPassword?: string | undefined
  avatar?: string | undefined
  phone: string
  email: string
  gender?: 0 | 1 | undefined
  userStatus?: 0 | 1 | undefined
  userRole?: 0 | 1 | undefined
}

interface EditUserParams {
  id: number
  userAccount: string
  username: string
  avatar?: string | undefined
  phone: string
  email: string
  gender?: 0 | 1 | undefined
  userStatus?: 0 | 1 | undefined
  userRole?: 0 | 1 | undefined
}

export const addUser = (data: AddUserParams) => {
  return request({
    url: '/manager/add',
    method: 'post',
    headers: {
      'Content-Type': 'application/json', // 明确指定请求格式
    },
    data: JSON.stringify(data), // 确保数据序列化
  })
}

export const editUser = (data: EditUserParams) => {
  return request({
    url: '/manager/edit',
    method: 'put',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json',
    },
  })
}

// 添加文件上传接口
export const uploadAvatar = (file: File) => {
  const formData = new FormData()
  formData.append('file', file)

  return request({
    url: '/upload/avatar',
    method: 'post',
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    data: formData,
  })
}

// 删除用户接口
export const deleteUser = (id: number) => {
  return request({
    url: `/manager/delete/${id}`,
    method: 'delete',
  })
}

// 重置密码接口
export const resetUserPassword = (id: number) => {
  return request({
    url: `/manager/password/reset/${id}`,
    method: 'put',
  })
}

interface ChangePasswordParams {
  id: number
  currentPassword: string
  newPassword: string
}

// 修改密码接口
export const changePassword = (data: ChangePasswordParams) => {
  return request({
    url: '/manager/password/change',
    method: 'put',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json',
    },
  })
}

// 获取帖子列表
export const getPostList = () => {
  return request({
    url: '/post/list',
    method: 'get',
  })
}

// 删除帖子
export const deletePost = (postId: number, userId: number) => {
  return request({
    url: `/post/${postId}/${userId}`,
    method: 'delete',
  })
}

// 修改帖子状态
export const updatePostStatus = (id: number, status: number) => {
  return request({
    url: '/post/status',
    method: 'put',
    data: { id, status },
  })
}

// 获取帖子详情
export const getPostDetail = (id: number) => {
  return request({
    url: `/post/query/${id}`,
    method: 'get',
  })
}

// 根据类型获取帖子列表
export const getPostListByType = (typeId: number) => {
  return request({
    url: `/post/list/type/${typeId}`,
    method: 'get',
  })
}

// 搜索帖子
export const searchPosts = (keyword: string, userId: number) => {
  return request({
    url: '/search',
    method: 'get',
    params: {
      keyword,
      userId,
    },
  })
}

// 获取被举报帖子列表
export const getReportedPosts = () => {
  return request({
    url: '/post/reported/list',
    method: 'get',
  })
}

// 通过举报（帖子没问题）
export const approveReport = (reportId: number, handleResult: string) => {
  return request({
    url: `/post/report/approve/${reportId}`,
    method: 'post',
    params: {
      handleResult,
    },
  })
}

// 处理举报（删除违规帖子）
export const rejectReport = (reportId: number, handleResult: string) => {
  return request({
    url: `/post/report/reject/${reportId}`,
    method: 'post',
    params: {
      handleResult,
    },
  })
}

// 获取待审核帖子列表
export const getPendingPosts = () => {
  return request({
    url: '/post/pending/list',
    method: 'get',
  })
}

// 通过帖子审核
export const approvePost = (postId: number, adminId: number) => {
  return request({
    url: `/post/approve/${postId}/${adminId}`,
    method: 'post',
  })
}

// 拒绝帖子审核
export const rejectPost = (postId: number, adminId: number, reason: string) => {
  return request({
    url: `/post/reject/${postId}/${adminId}`,
    method: 'post',
    params: {
      reason,
    },
  })
}

// 获取仪表盘数据
export const getDashboardData = () => {
  return request({
    url: '/dashboard',
    method: 'get',
  })
}

// 获取版本列表
export const getVersionList = () => {
  return request({
    url: '/admin/config/list',
    method: 'get',
  })
}

// 获取最新版本信息
export const getLatestVersion = (params?: { currentVersion?: string; platform?: string }) => {
  return request({
    url: '/version',
    method: 'get',
    params,
  })
}

// 添加新版本
export const addVersion = (data: {
  version: string
  platform: string
  downloadUrl: string
  description: string
  forceUpdate: number
  releaseTime: string
  status?: number
}) => {
  return request({
    url: '/admin/config/add',
    method: 'post',
    data,
  })
}

// 更新版本
export const updateVersion = (data: {
  id: number
  version?: string
  platform?: string
  downloadUrl?: string
  description?: string
  forceUpdate?: number
  releaseTime?: string
  status?: number
}) => {
  return request({
    url: '/admin/config/update',
    method: 'put',
    data,
  })
}

// 删除版本
export const deleteVersion = (id: number) => {
  return request({
    url: `/admin/config/delete/${id}`,
    method: 'delete',
  })
}

// 上传APK文件
export const uploadApkFile = (file: File) => {
  const formData = new FormData()
  formData.append('file', file)

  return request({
    url: '/upload/apk',
    method: 'post',
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    data: formData,
  })
}
